//
//  UserCardTable.m
//  blackjack
//
//  Created by Thi Nguyen Tran on 3/24/10.
//  Copyright 2010 Epsilon. All rights reserved.
//

#import "UserCardTable.h"

#define START_POS_X		38
#define START_POS_Y		23
#define BTN_WIDTH		61
#define BTN_HEIGHT		64

#define LABEL_WIDTH		400
#define LABEL_HEIGHT	20
#define SCREEN_WIDTH	320
#define RANGE			10

@implementation UserCardTable

@synthesize delegate;


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIImage *img = [UIImage imageNamed:@"basic_user_num.png"];
		[self setImage:img];
		
		textLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, LABEL_WIDTH, LABEL_HEIGHT)];
		[textLb setTextColor:[UIColor colorWithRed:200 green:100 blue:25 alpha:100]];
		[textLb setBackgroundColor:[UIColor clearColor]];
		[textLb setText:@"Select the value below corresponding to your cards"];
		[self addSubview:textLb];
		
		timer = [NSTimer scheduledTimerWithTimeInterval:0.1f 
												 target:self 
											   selector:@selector(runAnimation:) 
											   userInfo:nil 
												repeats:YES];
    }
    return self;
}

- (void)runAnimation:(NSTimer *)t {
	static int sign = -1;
	CGRect frame = [textLb frame];
	CGPoint origin = frame.origin;
	int x = origin.x + 3 * sign;
	int y = origin.y;
	if(x + LABEL_WIDTH < SCREEN_WIDTH || x > RANGE) {
		//x = SCREEN_WIDTH;
		sign = -sign;
	}
	[textLb setFrame:CGRectMake(x, y, LABEL_WIDTH, LABEL_HEIGHT)];
}

- (void)resetTextPosition {
	CGRect frame = [textLb frame];
	CGPoint origin = frame.origin;
	int x = RANGE;
	int y = origin.y;
	[textLb setFrame:CGRectMake(x, y, LABEL_WIDTH, LABEL_HEIGHT)];
}

- (void)touchesBegan:(NSSet *)touches 
		   withEvent:(UIEvent *)event {
	[self setUserInteractionEnabled:NO];
	UITouch * touch = [touches anyObject];
	CGPoint point = [touch locationInView:[touch view]];
	int i = (point.x - START_POS_X) / BTN_WIDTH;
	int j = (point.y - START_POS_Y) / BTN_HEIGHT;
	int res = i + j * 4;
	if(res >= 0 && res < 28 && i >= 0 && j >= 0) {
		[delegate chooseValueForUser:res];
	}
	else {
		[delegate chooseValueForUser:-1];
	}
}


- (void)dealloc {
    [super dealloc];
}


@end
